import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'

Vue.use(VueRouter)

// const routes

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    { path: '/', redirect: '/login' },
    { path: '/home', component: Home }
  ]
})

// 挂载路由导航守卫 beforeEach((to,from,next)=>{})
router.beforeEach((to, from, next) => {
  // to 是将要访问的地址,去哪
  // from 代表是从哪来的
  // next 是一个函数，next()表示放行,  next('/login')表示强制跳转
  if (to.path == '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
